import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_furnace/lib_furnace.dart';
export 'gt_dialog_button.dart';

class GTDialog {
  static Future<T?> show<T>({
    required BuildContext context,
    required List<Widget> actions,
    String? title,
    String? message,
    bool barrierDismissible = true,
    Color? barrierColor = const Color(0x80000000),
    String? barrierLabel,
    bool useSafeArea = true,
    bool? useRootNavigator,
    RouteSettings? routeSettings,
    @Deprecated(
        "已废弃，修改弹窗圆角用radius") ShapeBorder dialogShape = const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8))),
    double? radius,
    TextStyle? titleStyle,
    TextStyle? messageStyle,
    CrossAxisAlignment? textAlignment,
    Color? backgroundColor,
    Widget? customWidget,
    VoidCallback? onClose,
    double? marginHorzintal
  }) {
    return showDialog<T?>(
      context: context,
      barrierDismissible: barrierDismissible,
      barrierColor: barrierColor,
      barrierLabel: barrierLabel,
      useSafeArea: useSafeArea,
      useRootNavigator: useRootNavigator ?? true,
      routeSettings: routeSettings,
      builder: (context) {
        return Material(
          color: Colors.transparent,
          child: Center(
            child: Container(
              margin: EdgeInsets.symmetric(
                  horizontal: marginHorzintal ?? 24),
              decoration: BoxDecoration(
                color: GTColorsV3.cmpt16.color,
                borderRadius: BorderRadius.circular(radius ?? 8),
              ),
              child: GTDialogView(
                title: title,
                message: message,
                actions: actions,
                titleStyle: titleStyle,
                messageStyle: messageStyle,
                contentAlignment: textAlignment,
                customWidget: customWidget,
                onClose: onClose,
              ),
            ),
          ),
        );
      },
    );
  }
}

class GTDialogView extends StatelessWidget {
  const GTDialogView({
    super.key,
    required this.actions,
    this.title,
    this.titleStyle,
    this.contentAlignment,
    this.message,
    this.messageStyle,
    this.customWidget,
    this.onClose,
  });

  final List<Widget> actions;

  final String? title;

  final String? message;

  final TextStyle? titleStyle;

  final TextStyle? messageStyle;

  final CrossAxisAlignment? contentAlignment;

  final Widget? customWidget;

  final VoidCallback? onClose;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Column(
          crossAxisAlignment: contentAlignment ?? CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      if (title != null)
                        Padding(
                          padding: GTEdgeInsetsFormRTLB(24, 24, 0, 0),
                          child: Text(
                            title!,
                            style: titleStyle ??
                                TextStyle(
                                  fontWeight: FontWeight.w500,
                                  fontSize: 16,
                                  color: GTColorsV3.text1.color,
                                ),
                          ),
                        ),
                      if (message != null)
                        Padding(
                          padding: GTEdgeInsetsFormRTLB(24, 16, 0, 4),
                          child: Text(
                            message!,
                            style: messageStyle ??
                                TextStyle(
                                  fontSize: 14,
                                  color: GTColorsV3.text2.color,
                                ),
                          ),
                        ),
                    ],
                  ),
                ),
                onClose != null
                    ? GestureDetector(
                  onTap: onClose!,
                  behavior: HitTestBehavior.opaque,
                  child: Padding(
                    padding: GTEdgeInsetsOnly(left: 12, right: 16, top: 16),
                    child: Icon(
                      GTIconFont.e89b,
                      color: GTColorsV3.icon3.color,
                      size: 16,
                    ),
                  ),
                )
                    : const SizedBox(width: 24),
              ],
            ),
            if (customWidget != null) customWidget!,
            buildButtons(context),
          ],
        ),
      ],
    );
  }

  Widget buildButtons(BuildContext context) {
    return Padding(
      padding: GTEdgeInsetsFormRTLB(24, 16, 24, 24),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List.generate(
          actions.length,
              (index) {
            return Expanded(
              child: Padding(
                padding: GTEdgeInsetsOnly(
                    right: index < actions.length - 1 ? 8.0 : 0.0),
                child: actions[index],
              ),
            );
          },
        ),
      ),
    );
  }
}
